package chamans.away
{
	import away3d.containers.ObjectContainer3D;
	import away3d.materials.*;
	import away3d.entities.Mesh;
	import flash.geom.*;
	import com.bulletflash.dynamics.*;
	
	public class Room
	{
		private var BASE:Object;
		private var end:Function;
		private var Pos:Vector3D;
		private var matListe : Vector.<DefaultMaterialBase>;
		
		public function Room(base:Object, END:Function=null, P:Vector3D=null)
		{
			BASE=base; end=END || new Function(); 
			matListe=new Vector.<DefaultMaterialBase>();
			BASE.LOAD.IMGLIST("assets/demobullet/", ['floor', "floor_n", "floor_s", "panel_d2", "panel_local", "panel_s", "glass", "floor2" , "floor2_n", "floor2_s"  ], initMaterials , ".jpg")
		}
		private function initMaterials( A:Array  ) : void
		{
			var	matLight:ColorMaterial= new ColorMaterial(0xFFFFFF,.5);
			var	matVitre:BitmapMaterial= new BitmapMaterial(A[6],true,true,true);
			matVitre.name="room vitre 1"
			matVitre.transparent=true;
			matVitre.alpha=.2;
			matVitre.ambientColor = BASE.FogColor;
			matVitre.specularMap=A[6];
			matVitre.bothSides=true;
			matVitre.lights = BASE.LIGHT;
			var matVitre2:BitmapMaterial= new BitmapMaterial(A[6],true,true,true);
			matVitre2.name="room vitre 2"
			matVitre2.transparent=true;
			matVitre2.alpha=.4;
			matVitre2.ambientColor = BASE.FogColor;
			matVitre2.specularMap=A[6];
			matVitre2.bothSides=true;
			matVitre2.lights = BASE.LIGHT;
			matVitre2.specular =2;
			matVitre.specular =2;
			matVitre.ambient = 0.2;
			matVitre2.ambient = 0.2;
			matVitre2.gloss =50;
			matVitre.gloss =50;
			matVitre2.scaleU=10
			matVitre2.scaleV=3
			var	matground:BitmapMaterial = new BitmapMaterial(A[7],true,true,true);
			matground.name="room floor"
			matground.lights = BASE.LIGHT;
			matground.specular =1.1;
			matground.gloss =60;
			matground.ambient = 0.2;
			matground.ambientColor = BASE.FogColor;
			matground.normalMap=A[8];
			matground.specularMap=A[9];
			var	matBeton:BitmapMaterial = new BitmapMaterial(A[0],true,true,true);
			matBeton.name="room concret"
			matBeton.lights = BASE.LIGHT;
			matBeton.specular =.6;
			matBeton.gloss =20;
			matBeton.ambient = 0.2;
			matBeton.ambientColor = BASE.FogColor;
			matBeton.normalMap=A[1];
			matBeton.specularMap=A[2];
			
			var	matwall:BitmapMaterial = new BitmapMaterial(A[3],true,true,true);
			matwall.name="room wall"
			matwall.lights = BASE.LIGHT;
			matwall.specular =1.1;
			matwall.gloss =30;
			matwall.ambient = 0.2;
			matwall.ambientColor = BASE.FogColor;
			matwall.normalMap=A[4];
			
			var	matMetal:ColorMaterial = new ColorMaterial(0x505050);
			matMetal.name="room metal 1"
			matMetal.lights = BASE.LIGHT;
			matMetal.specular =.8;
			matMetal.gloss =50;
			matMetal.ambient = 0.2;
			matMetal.ambientColor = BASE.FogColor;
			var	matMetal2:ColorMaterial = new ColorMaterial(0x221411);
			matMetal2.name="room metal 2"
			matMetal2.lights = BASE.LIGHT;
			matMetal2.specular =.8;
			matMetal2.gloss =50;
			matMetal2.ambient = 0.2;
			matMetal2.ambientColor = BASE.FogColor;
			
			matwall.specularMap=A[5];
			
			BASE.F_reflect(matground, .3);
			BASE.F_reflect(matVitre, .6);
			BASE.F_reflect(matVitre2, .6);
			
			BASE.F_Fog(matground);
			BASE.F_Fog(matwall);
			BASE.F_Fog(matBeton);
			BASE.F_Fog(matMetal);
			BASE.F_Fog(matMetal2);
			BASE.F_Fog(matVitre);
			BASE.F_Fog(matVitre2);
			
			matListe.push(matground, matwall, matLight, matVitre, matVitre2, matBeton,matMetal,matMetal2 )
			BASE.MatListe.push(matground, matwall, matBeton, matMetal, matMetal2, matVitre, matVitre2);
			BASE.OBJ3D('assets/demobullet/room2.obj', Define)
		}
		private function Define(obj:ObjectContainer3D) : void
		{
			var mesh : Mesh;
			
			for (var i:int = 0; i < obj.numChildren; ++i) {
				mesh = Mesh(obj.getChildAt(i));
				if(i==0){mesh.material=matListe[0]; mesh.geometry.scaleUV(4);}
				if(i==3) {mesh.material=matListe[1];mesh.castsShadows=false;mesh.geometry.scaleUV(1);}
				
				if( i==1 || i==2 || i==7)mesh.material=matListe[6];
				if(i==8)mesh.material=matListe[7];
				if(i==4 || i==5) mesh.material=matListe[2];
				if(i==6 ){ mesh.material=matListe[3];mesh.castsShadows=false;}
				if( i==9){ mesh.material=matListe[4];mesh.castsShadows=false;}
				if( i==10){ mesh.material=matListe[5]; mesh.castsShadows=false;}
				if( i==12 || i==13){ mesh.material=matListe[0];mesh.castsShadows=false;mesh.geometry.scaleUV(2);}
				if( i==11 ){ mesh.material=matListe[5];mesh.castsShadows=false;}
				if(i==10 ) mesh.material=matListe[5];	
			}
			if(BASE.phyOn) BASE.physics.RoomPhysics(4000, 4000, 3000, 200);
			BASE.view.scene.addChild(obj);
			end();end=null;
		}
		private function RanPos() : Vector3D {
			return new Vector3D(BASE.TOOL.Ran(1000, -1000), 43, BASE.TOOL.Ran(1000));
		}
		public function Delete() : void
		{   
			//	 	if(O.phyOn) O.physics.Delete( m_liste, r_liste);
		}
		
		
	}}
